﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mitasoft.Web.DAL.Model;
using FizzWare.NBuilder;
using mitasoft.Web.DAL;
using MySite2.ViewModel;

namespace MySite2.Areas.RedZone.Controllers
{
    public class BookController : Controller
    {

        BooksViewModel _booksViewModel;

        public BookController()
        {
            _booksViewModel = new BooksViewModel();
        }



        public ActionResult Index(int? pageNumber)
        {
            _booksViewModel.GetBooks(pageNumber ?? 1);
            return View(_booksViewModel);
        }

        public ActionResult Details(int id)
        {
            var book = _booksViewModel.GetBook(id);
            return View(book);
        }

        //
        // GET: /RedZone/Book/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /RedZone/Book/Create

        [HttpPost]
        public ActionResult Create(Book book)
        {
            try
            {
                _booksViewModel.AddBook(book);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /RedZone/Book/Edit/5

        public ActionResult Edit(int id)
        {
            return View(_booksViewModel.GetBook(id));
        }

        //
        // POST: /RedZone/Book/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Book book)
        {
            try
            {
                _booksViewModel.EditBook(book);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /RedZone/Book/Delete/5

        public ActionResult Delete(int id)
        {
            return View(_booksViewModel.GetBook(id));
        }

        //
        // POST: /RedZone/Book/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                _booksViewModel.RemoveBook(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
